﻿package encrypt {
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class Xor {

		public function Xor() {
			// constructor code
		}
		public static function encrypt(key:ByteArray , data:ByteArray):ByteArray
		{
			var ret:ByteArray =  new ByteArray();
			ret.endian = Endian.LITTLE_ENDIAN;
			var keyLength:int =  key.length;
			var val:int = 0;
			for(var i:int = 0 ; i < data.length ; i++)
			{
				var k_i:int = i % keyLength;
				val = (val ^ data[i] ^ key[k_i]);
				ret.writeByte(val);
			}
			return ret;
		}
		public static function decrypt(key:ByteArray , data:ByteArray):ByteArray
		{
			var ret:ByteArray = new ByteArray();
			ret.endian = Endian.LITTLE_ENDIAN;
			var keyLength:int = key.length;
			var val_i:int = 0;
			var val_i_1:int = 0;
			for(var i:int = 0 ; i< data.length ; i++)
			{
				val_i_1 = val_i;
				val_i = data[i];
				var k_i:int = i % keyLength;
				var val:int = val_i_1 ^ data[i] ^ key[k_i];
				ret.writeByte(val);
			}
			return ret;
		}
		

	}
	
}
